<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="适用门店">
              <a-input placeholder="" v-model="queryParam.orgName" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
        </span>
        <span slot="bizStatus" slot-scope="text, record">
          <a-switch v-model="record.bizStatus" @change="handleStatus(record)" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <posOrderAdjustRule-modal ref="modalForm" @ok="modalFormOk"></posOrderAdjustRule-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import PosOrderAdjustRuleModal from './modules/PosOrderAdjustRuleModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction, putAction } from '@/api/manage'

export default {
  name: 'PosOrderAdjustRuleList',
  mixins: [JeecgListMixin],
  components: {
    PosOrderAdjustRuleModal,
  },
  data() {
    return {
      description: '手工优惠规则管理页面',
      // 表头
      columns: [
        {
          title: '适用门店',
          align: 'center',
          dataIndex: 'orgName',
          customRender:(text)=>{
            return text + '/全部门店'
          }
        },
        {
          title: '金额(元)',
          align: 'center',
          dataIndex: 'ruleVal',
          customRender: (text, record) => {
            if (record.ruleType === 1) {
              return '1-' + parseFloat(text) / 100
            } else {
              return '-'
            }
          },
        },
        {
          title: '百分比',
          align: 'center',
          dataIndex: 'ruleVal1',
          customRender: (text, record) => {
            if (record.ruleType === 2) {
              return '1-' + parseFloat(record.ruleVal)
            } else {
              return '-'
            }
          },
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/cashier/posOrderAdjustRule/list',
        edit: '/cashier/posOrderAdjustRule/edit',
      },
    }
  },
  computed: {},
  methods: {
    handleStatus(record) {
      this.loading = true
      let toModify = {}
      toModify.id = record.id
      toModify.bizStatus = record.bizStatus ? 1 : 0
      putAction(this.url.edit, toModify)
        .then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
          } else {
            this.$message.warn(res.message)
          }
        })
        .finally(() => (this.loading = false))
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>